<?php

namespace app\admin\controller\mul;

use app\common\controller\Backend;
use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Missionemployee extends Backend
{
    
    /**
     * MulMissionEmployee模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('MulMissionEmployee');

    }


    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where=array();
            $inputs=input();
            $input = json_decode($inputs['filter'], true);
            if(!empty($input['id'])){
                $where['id']=$input['id'];
            }
            if(!empty($input['user_id'])){
                $user_id=DB::name('users')->where(['nickname'=>array('like','%'.$input['user_id'].'%')])->value('user_id');
                $where['user_id']=array('in',$user_id);
            }
            if(!empty($input['mission_id'])){
                $mission_id=DB::name('mul_mission')->where(['title'=>array('like','%'.$input['mission_id'].'%')])->value('id');
                $where['mission_id']=array('in',$mission_id);
            }
            if(!empty($input['status'])){
                $where['status']=$input['status'];
            }
            if(!empty($input['company_name'])){
                $where['company_name']=array('like','%'.$input['boss_info'].'%');
            }
            if(!empty($input['create_time'])) {
                $date = explode(',', $input['create_time']);
                foreach($date as &$v){
                    $v = strtotime($v);
                }
                $where['create_time'] = array('between', $date);
            }
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $data=array(
                '1'=>'已报名',
                '2'=>'平台已确认',
                '3'=>'金主已确认',
                '4'=>'任务未完成',
                '5'=>'任务落选',
                '6'=>'已结账'
            );
            foreach($list as &$l){
                $l['status']=$data[$l['status']];
                $l['mission_id']=DB::name('MulMission')->where('id='.$l['mission_id'])->value('title');
                $l['user_id']=DB::name('Users')->where('user_id='.$l['user_id'])->value('nickname');
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $btns = array('refresh');

        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
    //任务选中或结账
    function modify($id){
        if($id){

            $list=$this->model
                ->where('id='.$id)
                ->find();

            if($list['status']==1 || $list['status']==3){
                Db::startTrans();
                try {
                        if ($list['status'] == 1) {
                            $list->save(array('verify_time'=>time(),'status'=>2));
                            // 提交事务
                            Db::commit();
                            return json(array('code' => 200, 'msg' => '平台确认成功'));

                        } elseif ($list['status'] == 3) {
                            //查找悬赏金额
                            $mission=Db::name('mul_mission')->where('id='.$list['mission_id'])->find();

                            if($mission['remain_num']==0){
                                DB::name('MulMission')->where('id='.$list['mission_id'])->update(['status'=>2]);
                            }
                            $list->save(array('pay_time'=>time(),'status'=>6));
                            //给用户增加余额
                            Db::name('users')->where('user_id='.$list['user_id'])->setInc('user_money',$mission['fee']);
                            //添加记录
                            $data=array(
                                'user_id' => $list['user_id'],
                                'amount' => $mission['fee'],
                                'add_time' => time(),
                                'paid_time' => time(),
                                'user_note' => '发布悬赏任务;任务编ID:'.$list['id'],
                                'payment' => '用户余额增加',
                                'process_type' => 13,
                                'is_paid' => 1
                            );
                            Db::name('user_echarge')->insert($data);
                            // 提交事务
                            Db::commit();
                            return array('code' => 200, 'msg' => '付款成功');
                        }


                }catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return array('code' => -200, 'msg' => '平台确认失败');
                }
            }else{
                return array('code'=>-200,'msg'=>'该数据待用户确认');
            }
        }
    }
    //地图页
    public function dt(){
        $ids=input('ids');
        $list=$this->model
            ->where('id='.$ids)
            ->find();
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    //任务落选
    public function jj($id){
        $list=$this->model
            ->where('id='.$id)
            ->find();
        Db::startTrans();
        try {
            $list->save(['status'=>5]);
            $mission=Db::name('MulMission')->where('id='.$list['mission_id'])->find();
            Db::name('MulMission')->where('id='.$list['mission_id'])->setInc('remain_num');
            // 提交事务
            Db::commit();
            return array('code'=>200,'msg'=>'已拒绝');
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return array('code' => -200, 'msg' => '平台拒绝失败');
        }
    }

}
